/**
 * 
 */
package hu.radai.jsfportal.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

/**
 * @author Radai Tamas
 */
public class Constants {
	
	//==========================================================================
	
	/** Fejleszto mod: naplozashoz, stb. */
	public static Boolean isDevelopMode;
	
	public static Properties properties;
	
	public static String propertiesFilePath;
	
	//==========================================================================
	
	/** Current working directory. Example: E:\develop\apache-tomcat-7.0.5\bin */
	public static final String USER_DIR = System.getProperty(SystemKeyConstants.USER_DIR);
	
	/** Path (file) separator. Example: '/' or '\' */
	public static final String FILE_SEPARATOR = System.getProperty(SystemKeyConstants.FILE_SEPARATOR);
	/** Path separator. Example: ':' or ';' */
	public static final String PATH_SEPARATOR = System.getProperty(SystemKeyConstants.PATH_SEPARATOR);
	/** System (new) line separator. Example: '\n' or '\r\n' */
	public static final String LINE_SEPARATOR = System.getProperty(SystemKeyConstants.LINE_SEPARATOR);
	
	/** Java class path. Example: 'E:\develop\apache-tomcat-7.0.5\bin\bootstrap.jar;E:\develop\...' */
	public static final String JAVA_CLASS_PATH = System.getProperty(SystemKeyConstants.JAVA_CLASS_PATH);
	/** List of paths to search when loading libraries. Example: 'C:\ProgramFiles\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;...' */
	public static final String JAVA_LIBRARY_PATH = System.getProperty(SystemKeyConstants.JAVA_LIBRARY_PATH);
	/** Path of extension directory or directories. Example: 'C:\Program Files\Java\jre6\lib\ext;C:\Windows\Sun\Java\lib\ext' */
	public static final String JAVA_EXT_DIRS = System.getProperty(SystemKeyConstants.JAVA_EXT_DIRS);
	
	//==========================================================================
	
	/** Tomcat base dir. E:\develop\apache-tomcat-7.0.5 */
	public static final String TOMCAT_BASE_DIR = USER_DIR.substring(0, USER_DIR.lastIndexOf(FILE_SEPARATOR + "bin"));
	/** Tomcat webapps dir with web application: E:\develop\apache-tomcat-7.0.5\webapps */
	public static final String TOMCAT_WEBAPPS_DIR = TOMCAT_BASE_DIR +
													FILE_SEPARATOR + "webapps";
	/** Web application config dir. Example: '\WEB-INF\classes\config' */
	public static final String WEBAPP_CONFIG_DIR = FILE_SEPARATOR + "WEB-INF" +
												   FILE_SEPARATOR + "classes" +
												   FILE_SEPARATOR + "config";
	
	//==========================================================================
	
	/** Develop mode logger config file for Log4J: \log4j-dev.xml */
	public static final String LOG_FILE_DEV = FILE_SEPARATOR + "log4j-dev.xml";
	/** Test mode logger config file for Log4J: \log4j-test.xml */
	public static final String LOG_FILE_TEST = FILE_SEPARATOR + "log4j-test.xml";
	/** Final logger config file for Log4J: \log4j.xml */
	public static final String LOG_FILE = FILE_SEPARATOR + "log4j.xml";
	/** Develop mode config file for webapp: \properties-dev.xml */
	public static final String CONFIG_FILE_DEV = FILE_SEPARATOR + "properties-dev.xml";
	/** Test mode config file for webapp: \properties-test.xml */
	public static final String CONFIG_FILE_TEST = FILE_SEPARATOR + "properties-test.xml";
	/** Final config file for webapp: \properties.xml */
	public static final String CONFIG_FILE = FILE_SEPARATOR + "sproperties.xml";
	
	//==========================================================================
	
	public static synchronized void init () {
		assert "".equals(propertiesFilePath) : "'propertiesFilePath' is NULL or EMPTY";
		assert new File(propertiesFilePath).exists() : "'propertiesFilePath' is not exists";
		try {
			properties = new Properties();
			properties.loadFromXML(new FileInputStream(new File(propertiesFilePath)));
		}
		catch (final InvalidPropertiesFormatException e) {
			e.printStackTrace();
		}
		catch (final FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (final IOException e) {
			e.printStackTrace();
		}
		finally {}
	}
	
	public synchronized static void release () {}
	
	public synchronized static void reload () {
		release();
		init();
	}
}
